package org.secangkirkopipanas.gxtcore.server.gwt;

import java.util.Date;

import javax.servlet.http.HttpSession;

import net.sf.hibernate4gwt.core.HibernateBeanManager;
import net.sf.hibernate4gwt.gwt.HibernateRemoteService;

import org.secangkirkopipanas.gxtcore.client.GxtCoreRemoteService;
import org.secangkirkopipanas.gxtcore.common.LoginResult;
import org.secangkirkopipanas.gxtcore.domain.ILoginResult;
import org.secangkirkopipanas.gxtcore.domain.ISystemUser;
import org.secangkirkopipanas.gxtcore.domain.dto.LoginResultDTO;
import org.secangkirkopipanas.gxtcore.server.ApplicationContext;
import org.secangkirkopipanas.gxtcore.server.service.ISystemUserService;

public class GxtCoreRemoteServiceImpl extends HibernateRemoteService implements
		GxtCoreRemoteService {

	private static final long serialVersionUID = 1L;

	private ISystemUserService systemUserService;

	public GxtCoreRemoteServiceImpl() {
		setBeanManager((HibernateBeanManager) ApplicationContext.getInstance()
				.getBean("hibernateBeanManager"));
		systemUserService = (ISystemUserService) ApplicationContext
				.getInstance().getBean(ISystemUserService.NAME);
	}

	public ILoginResult login(String username, String password) {

		HttpSession httpSession = getThreadLocalRequest().getSession(false);
		ILoginResult loginResult = null;

		ISystemUser systemUser = systemUserService.get(username, password);
		if (systemUser != null) {
			loginResult = new LoginResult(systemUser, new Date());
			httpSession.setAttribute("loginResult", loginResult);
		}

		return (LoginResultDTO) clone(loginResult);
	}

	public void logout() {
		// TODO Auto-generated method stub
	}

}